package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAddressMapper;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;

public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    /**
     * 查询系统管理 -- 管理员 分页
     * @param page
     * @param limit
     * @param username
     * @param sort
     * @param order
     * @return
     */

    @Override
    public IPage<DtsAdmin> selectPage(Integer page, Integer limit, String username, String sort, String order) {
        /* LambdaQueryWrapper<DtsAdmin> dtsAdminLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //用户名  对象属性
        dtsAdminLambdaQueryWrapper.like(DtsAdmin::getUsername,username);
        //排序
        dtsAdminLambdaQueryWrapper.orderBy(!StringUtils.isEmpty(sort),
                "asc".equalsIgnoreCase(order),DtsAdmin::getAddTime);*/

        // select * from dts_admin where username like '%username%' order by sort  desc
        QueryWrapper<DtsAdmin> queryWrapper = new QueryWrapper<>();
        //用户名 字段
        queryWrapper.like(!StringUtils.isEmpty(username),"username",username);

        //排序 Children orderBy(boolean condition, boolean isAsc, R... columns);
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort);

        Page<DtsAdmin> dtsAdminPage = dtsAdminMapper.selectPage(new Page<DtsAdmin>(page,limit),queryWrapper);
        return dtsAdminPage;
    }
    /**
     * 添加
     * @param dtsAdmin
     * @return
     */
    @Override
    public DtsAdmin create(DtsAdmin dtsAdmin) {


        dtsAdmin.setLastLoginTime(new Date());
        dtsAdmin.setDeleted(false);
        dtsAdmin.setAddTime(new Date());
        dtsAdmin.setUpdateTime(new Date());
        dtsAdminMapper.insert(dtsAdmin);
        return dtsAdmin;
    }
}
